<template>
  <div class="login">
    <div class="login-wrap">
      <div class="title">电梯风险数据管理系统</div>
      <!-- 表单开始 -->
      <el-form label-position="left" :model="loginForm" class="login-form" size="medium">
        <el-form-item class="login-item">
          <label>用户名</label>
          <el-input v-model="loginForm.userName"></el-input>
        </el-form-item>
        <el-form-item class="login-item">
          <label>密码</label>
          <el-input v-model="loginForm.passWord" show-password></el-input>
        </el-form-item>
        <el-form-item class="login-item">
          <el-button type="primary" class="btn-submit" @click="submitForm">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { reactive, getCurrentInstance } from "vue";
import { useRouter } from 'vue-router';
import { useStore } from 'vuex';
import sha1 from 'js-sha1';

export default {
  name: "Login",

  setup(){
    const { proxy } = getCurrentInstance() // 获取当前vue实例，类似于this
    const router = useRouter()
    const store = useStore()

    let loginForm = reactive({
      userName: 'test',
      passWord: 'secret'
    })

    let submitForm = (() => {
      if ((!loginForm.userName) || (!loginForm.passWord)) {
        proxy.$message.error('请输入用户名和密码！')
      }else{
        // loginForm.passWord = sha1(loginForm.passWord)

        store.dispatch('app/login', loginForm).then( response => {
          router.push({name: 'Console'})
        }).catch((error)=>{
          console.log(error);
        })
      }
    })

    return { loginForm, submitForm }
  }
}
</script>

<style lang="scss" scoped>
.login {
  background-color: #344a5f;
  position: absolute;
  height: 100%;
  width: 100%;

  .login-wrap {
    width: 350px;
    margin: 60px auto;
    border-radius: 35px;
    padding: 50px 100px;

    .title {
      color: #fff;
      font-size: 25px;
      text-align: center;
      margin: auto;
    }

    .login-form {
      margin-top: 30px;

      .login-item {
        padding: 5px;

        label {
          display: block;
          color: #fff;
          font-size: 14px;
          margin-bottom: 3px;
        }
      }

      .btn-submit{
        display: block;
        width: 100%;
        margin-top: 19px;
      }
    }
  }
}

</style>
